<?php
require_once("DataBase.inc");

class Query {
	
	private $main_table;
	private $selected_fields;
	private $where;
	private $joins;
	private $groups, $orders;
	
	public static $queries = array();
	
	public function __construct($main_table, $selected_fields=null) {
		$this->main_table = $main_table;
		$this->selected_fields = $selected_fields <> null ? $selected_fields : array();
		$this->where = "";
		$this->joins = array();
		$this->groups = array();
		$this->orders = array();
	}
	
	public static function query_sql($q) {
		$r = DataBase::query($q);
		array_push(self::$queries, array($q, DataBase::last_error_number(), DataBase::last_error_message(), $r === false ? 0 : count($r), DataBase::number_of_affected_rows()));
		return $r;
	}
	
	public function execute() {
		$q = $this->build_sql();
		$r = self::query_sql($q);
		return $r;
	}
	public function count() {
		if (count($this->groups)>0) {
			$r = $this->execute();
			if ($r === false) return 0;
			return count($r);

		}
		if (count($this->selected_fields)==0)
			$this->selected_fields = array("COUNT(*)"=>null);
		else {
			foreach ($this->selected_fields as $name=>$alias) {
				$this->selected_fields = array("COUNT(".$name.")"=>null);
				break;
			}
		}
		$r = $this->execute();
		return $r[0][0];
	}
        public function counts(){
            $counts = $this->execute();
            return $counts[0][0];
        }


        public function select($field, $alias=null) {
		$this->selected_fields[$field] = $alias;
	}
	
	public function where($w) {
		if ($this->where <> "") $this->where .= " AND ";
		$this->where .= "(".$w.")";
	}
	
	public function join($table, $alias, $conditions) {
		array_push($this->joins, array($table, $alias, $conditions));
	}
	
	public function join_applicant($table, $alias=null, $conditions=null) {
		if ($conditions == null) $conditions = array();
		array_push($conditions, ($alias == null ? $table : $alias).".applicant_id=applicant.applicant_id");
		$this->join($table, $alias, $conditions);
	}
	
	public function group($group) {
		array_push($this->groups, $group);
	}
	
	public function order($order) {
		array_push($this->orders, $order);
	}
	
	public function build_sql() {
		$q = "SELECT ";
		if (count($this->selected_fields)==0)
			$q .= "*";
		else {
			$first = true;
			foreach ($this->selected_fields as $name=>$alias) {
				if ($first) $first = false; else $q .= ",";
				$q .= $name;
				if ($alias <> null) $q .= " AS ".$alias;
			}
		}
		$q .= " FROM ".$this->main_table;
		foreach ($this->joins as $join) {
			$q .= " LEFT JOIN ".$join[0];
			if ($join[1] <> null) $q .= " AS ".$join[1];
			$q .= " ON (".self::build_conditions($join[2]).")";
		}
		if ($this->where <> "") $q .= " WHERE ".$this->where;
		if (count($this->groups) > 0) {
			$q .= " GROUP BY ";
			for ($i = 0; $i < count($this->groups); $i++) {
				if ($i > 0) $q .= ",";
				$q .= $this->groups[$i];
			}
		}
		if (count($this->orders) > 0) {
			$q .= " ORDER BY ";
			for ($i = 0; $i < count($this->orders); $i++) {
				if ($i > 0) $q .= ",";
				$q .= $this->orders[$i];
			}
		}
		return $q;
	}
	
	public static function build_conditions($conditions, $operator="AND") {
		$sql = "";
		foreach ($conditions as $cd) {
			if ($sql<>"") $sql .= " ".$operator." ";
			$sql .= $cd;
		}
		return $sql;
	}
	
}
?>